import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# 训练数据
x = np.array([[100], [113], [90], [89], [60], [70], [50], [45], [55], [78]])  # 房屋面积
y = np.array([[301], [324], [285], [296], [200], [260], [300], [120], [180], [245]])  # 房屋价格

# 创建并训练模型
model = LinearRegression()
model.fit(x, y)

# 预测训练数据
y_pred = model.predict(x)

# 测试数据
x_test = np.array([[103], [115], [90], [89], [60], [70], [50], [45], [55], [78]])
y_test = np.array([[301], [344], [275], [276], [206], [210], [160], [124], [190], [235]])

# 计算评估指标
mse = np.average((y_pred - y) ** 2)  # 均方误差
rmse = np.sqrt(mse)  # 均方根误差
r2 = model.score(x_test, y_test)  # 决定系数R²

# 输出评估指标
print("均方误差为：", mse)
print("均方根误差为：", rmse)
print("预测准确率为：", r2)